class Order < ActiveRecord::Base
  belongs_to :restaurant
  belongs_to :customer, :validate => true
  belongs_to :delivery_man
  
  validates_presence_of :state

  statelogic do # +:state+ is the default value, may be omitted
        # you get methods +unpaid?+ and +was_unpaid?+.
        # may be more than one initial state.
        initial_state 'incomplete' do
          transitions_to 'pending'
        end

        state 'pending' do
          transitions_to 'processing' # won't let you change to wrong states
          validates_presence_of :content, :date, :address
          validates_associated :restaurant
        end

        state 'processing' do # you get +ready?+, +was_ready?+, +Order.ready+ named scope and +Order.find_all_ready+ finder
          transitions_to 'shipping'
        end
        
        state 'shipping' do # likewise
          transitions_to 'delivered'
          validates_associated :delivery_man # scoped validations
          validates_each :delivery_man do |record, attr, value|
            if value.restaurant != record.restaurant
              record.errors.add attr, "Il fattorino non appartiene alla pizzeria dell'ordine!"
            end
          end
        end
        state 'delivered' do # you guess

        end
  end
end
